package com.autodatabase.json;

import java.util.Collection;

import org.json.JSONArray;
import org.json.JSONObject;

import com.autodatabase.model.FactoryItemInfo;

public class JsonUtil {
	public static String factoryItemsToTree(Collection<FactoryItemInfo> list) {
		JSONArray array = new JSONArray();
		int i = 1;

		for (FactoryItemInfo item : list) {
			JSONObject obj = new JSONObject();
			i++;

			try {
				obj.put("id", item.getKey());
				obj.put("text", item.getItemName());
				JSONObject attr = new JSONObject();
				attr.put("key", item.getKey());
				obj.put("attributes", attr);
				obj.put("iconCls", "factoryItem");
				obj.put("state", "closed");
				array.put(obj);
			}
			catch (Exception e) {
			}
		}
		
		return array.toString();
	}

	public static String catalogsToTree(String key, Collection<String> list) {
		JSONArray array = new JSONArray();
		
		for (String catalog : list) {
			JSONObject obj = new JSONObject();

			try {
				obj.put("id", "catalog" + "_" + catalog + "^" + key);
				obj.put("text", catalog);
				obj.put("iconCls", "catalog");
				obj.put("state", "closed");
				array.put(obj);
			}
			catch (Exception e) {
			}
		}
		
		return array.toString();
	}
	
	public static String tablesToTree(Collection<String> list) {
		JSONArray array = new JSONArray();
		
		for (String table : list) {
			JSONObject obj = new JSONObject();

			try {
				obj.put("id", "table" + "_" + table);
				obj.put("text", table);
				obj.put("iconCls", "table");
				array.put(obj);
			}
			catch (Exception e) {
			}
		}
		
		return array.toString();
	}
}
